package com.ease.database.initializer;

import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.ease.common.util.Detect;
import com.ease.common.util.FileUtil;
import com.ease.common.vo.MapParameter;
import com.ease.dao.JdbcTemplateDataAccessObject;
import com.obas.test.ApplicationContextUserContextTest;

/**
 * @author nathanleewei
 *
 */
public class ElementDeviceInitializer extends ApplicationContextUserContextTest {

	@Autowired
	JdbcTemplateDataAccessObject jdbcTemplateDataAccessObject;

	@Test
	public void test() {
		String string = FileUtil.readClassPathResourceToString(ElementDeviceInitializer.class, "MAC_TABLE_IP.DAT");

		String[] rows = StringUtils.delimitedListToStringArray(string, "\n");
		MapParameter parameter = null;
		if (null != rows) {
			for (String row :rows) {
				String[] values = StringUtils.delimitedListToStringArray(row, " ");
				if (null == values || values.length < 2) {
					continue;
				}
				String ipAddress = Detect.trim(values[0]);
				String equipmentModelCode = Detect.trim(values[1]);
				String name = Detect.trim(values[2]);

				parameter = new MapParameter();
				parameter.put("name", name);
				parameter.put("ipAddress", ipAddress);
				parameter.put("equipmentModelCode", equipmentModelCode);
				//
				List<Map<String, Object>> timeDimensions = jdbcTemplateDataAccessObject.find("SELECT * FROM I_ELEMENT_DEVICE T WHERE T.NAME=:name", parameter);
				if (!notEmpty(timeDimensions)) {
					

					jdbcTemplateDataAccessObject
							.create("INSERT INTO I_ELEMENT_DEVICE(NAME, IPADDRESS, EQUIPMENTMODELCODE)VALUES(:name, :ipAddress, :equipmentModelCode)",
									parameter);
				}
				
				//
				timeDimensions = jdbcTemplateDataAccessObject.find("SELECT * FROM M_EQUIPMENTMODEL T WHERE T.NAME=:equipmentModelCode", parameter);
				if (!notEmpty(timeDimensions)) {
					
					jdbcTemplateDataAccessObject
							.create("INSERT INTO M_EQUIPMENTMODEL(id, name, code,  version , equipmentvendor_id)VALUES(seq_M_EQUIPMENTMODEL.nextval, :equipmentModelCode, :equipmentModelCode, 1,1)",
									parameter);
				}
				
			}
		}

	}

}
